package com.biz.primus.ms.commodity.dao.po;

import com.biz.primus.base.enums.commodity.IStatus;
import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.commodity.vo.backend.IBrandVo;
import com.biz.primus.common.utils.PingYinUtil;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 品牌 Po
 *
 * @author david-liu
 * @date 2016年12月14日
 * @reviewer
 * @see
 */
@Data
@Entity
@Table(name = "pro_brand")
public class Brand extends BaseEntity implements Serializable {

	private static final long serialVersionUID = -8962025626015252979L;

    /**
     * 名称
     */
    @Column(nullable = false, length = 50)
    private String name;

    /**
     * 首字母
     */
    @Column(length = 5)
    private String prefix;

    /**
     * 品牌 Logo
     */
    @Column
    private String logo;

    /**
     * 品牌描述
     */
    @Column
    private String note;

    /**
     * 品牌排序
     */
    @Column
    private Integer idx;

    /**
     * 状态
     */
    @Column
    @Convert(converter = IStatus.Converter.class)
    private IStatus status;

    /**
     * 品牌分类
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @OrderBy(value = "idx")
    @JoinTable(name = "pro_category_brand",
            joinColumns = {@JoinColumn(name = "brand_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")},
            uniqueConstraints = {@UniqueConstraint(columnNames= {"brand_id" , "category_id"})})
    private List<Category> categories;

    /**
     * 品牌商品
     */
    @OneToMany
    private List<Product> products;

    /**
     * 品牌商品审核记录
     */
    @OneToMany
    private List<ProductAudit> productAudits;

    /**
     * 品牌渠道
     */
    @Convert(converter = SaleChannel.Converter.class)
    private SaleChannel saleChannel;
    /**
     * 删除标识
     */
    @Column
    private Boolean deleteFlag = Boolean.FALSE;

    public void fromVo(IBrandVo vo) {
        this.setStatus(vo.getStatus());
        this.setIdx(vo.getIdx());
        this.setLogo(vo.getLogo());
        this.setName(vo.getName());
        this.setPrefix(vo.getPrefix());
        this.setSaleChannel(vo.getSaleChannel());
        this.setPrefix(PingYinUtil.getFirstLetter(this.getName()));
    }

    @Override
    public String toString() {
        return "Brand{" +
                "name='" + name + '\'' +
                ", prefix='" + prefix + '\'' +
                ", logo='" + logo + '\'' +
                ", idx=" + idx +
                ", status=" + status +
                ", categories=" + categories +
                ", products=" + products +
                ", productAudits=" + productAudits +
                ", saleChannel=" + saleChannel +
                ", deleteFlag=" + deleteFlag +
                '}';
    }
}
